package com.cn.zpark;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: com.cn.zpark
 * @ClassName: ServletContextDemo01
 * @Description:
 * @author: RZS
 * @date: 2022/4/8  10:43
 */
@WebServlet("/scd1.do")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 获取servletContext对象的方法
         */
        // 1. 通过 GenericServlet 提供的 getServletContext() 方法
        ServletContext context1 = this.getServletContext();
        System.out.println(context1);

        // 2. 通过 ServletConfig 提供的 getServletContext() 方法
        ServletContext context2 = this.getServletConfig().getServletContext();
        System.out.println(context2);

        // 3. 通过 HttpSession 提供的 getServletContext() 方法**
        ServletContext context3 = request.getSession().getServletContext();
        System.out.println(context3);

        // 4. 通过 HttpServletRequest 提供的 getServletContext() 方法**
        ServletContext context4 = request.getServletContext();
        System.out.println(context4);
    }
}
